package com.huawei.lynnbit.tool.cocos.util;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;

public class JsonUtils {
    private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();

    static {
        OBJECT_MAPPER.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    }

    public static <T> T toEntity(String content, Class<T> type) throws JsonProcessingException {
        return OBJECT_MAPPER.readValue(content, type);
    }

    public static <T> T toEntity(String content, JavaType type) throws JsonProcessingException {
        return OBJECT_MAPPER.readValue(content, type);
    }

    public static JavaType getCollectionType(Class<?> collectionClass, Class<?>... elementClass) {
        return OBJECT_MAPPER.getTypeFactory().constructParametricType(collectionClass, elementClass);
    }
}
